﻿using LibraryManagement.Custom_Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibraryManagement.Security
{
    public partial class NewAccount : libForm
    {
        private SecAccountDetail OSecAccountDetail;
        public enum Redirection { Add = 1, Edit = 2 }
        public Redirection eRedirection;
        private LibManagementEntities context;
        private decimal AccountId;
        private Redirection redirection;
        public NewAccount()
        {
            InitializeComponent();
            eRedirection = Redirection.Add;
        }

        public NewAccount(Redirection redirection)
        {
            InitializeComponent();
            this.redirection = redirection;
            context = new LibManagementEntities();
            FillControls();
        }

        private void FillControls()
        {
            txtLoginId.Enabled = false;
            txtPwd.Enabled = false;
            txtPwdReEntered.Enabled = false;
            OSecAccountDetail = context.SecAccountDetails.Single(x => x.AccountId == clsStatics.currentUserId);
            txtFirstName.Text = OSecAccountDetail.FirstName;
            txtLastName.Text = OSecAccountDetail.LastName;
            txtEmail.Text = OSecAccountDetail.Email;
            txtEmailPwd.Text = OSecAccountDetail.EmailPassword;
            dtpDateOfBirth.Value = (DateTime)OSecAccountDetail.DOB;
        }

        private void brnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            context = new LibManagementEntities();
            if (eRedirection == Redirection.Add)
                OSecAccountDetail = new SecAccountDetail();
            else if (eRedirection == Redirection.Edit)
                OSecAccountDetail = context.SecAccountDetails.Single(x => x.AccountId == AccountId);
            if (eRedirection == Redirection.Add)
            {
                OSecAccountDetail.LoginId = txtLoginId.Text;
                OSecAccountDetail.Password = clsEncyrption.AES.Encrypt(txtPwd.Text, "tristana#18");
            }
            OSecAccountDetail.FirstName = txtFirstName.Text;
            OSecAccountDetail.LastName = txtLastName.Text;
            OSecAccountDetail.Email = txtEmail.Text;
            OSecAccountDetail.EmailPassword = clsEncyrption.AES.Encrypt(txtEmailPwd.Text, "tristana#18");
            OSecAccountDetail.DOB = dtpDateOfBirth.Value.Date;
            if (eRedirection == Redirection.Add)
            {
                context.SecAccountDetails.Add(OSecAccountDetail);
            }
            context.SaveChanges();
            this.Close();
        }
    }
}
